package com.cqhilink.iot.jobstandardmanagement.services.innerservice.permission.util;

import com.cqhilink.iot.jobstandardmanagement.common.bean.ApplicationContextHelper;
import com.cqhilink.iot.jobstandardmanagement.facade.model.AccountRole;
import com.cqhilink.iot.jobstandardmanagement.facade.webservicesmodel.UasUserRole;
import com.cqhilink.iot.jobstandardmanagement.facade.service.permission.JsmAccountRoleService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/2/17.
 */
public class AccountRolePool implements Runnable {
    private List<UasUserRole> list;
    private String controlType;//1.添加，2.修改，3.删除

    public void setList(List<UasUserRole> list) {
        this.list = list;
    }

    public void setControlType(String controlType) {
        this.controlType = controlType;
    }

    @Override
    public void run() {
        JsmAccountRoleService accountRoleService = (JsmAccountRoleService) ApplicationContextHelper.getBean("accountRoleService");
        if (accountRoleService != null && StringUtils.isNotEmpty(controlType)) {
            List<AccountRole> paramList = new ArrayList<AccountRole>();
            try {
                accountRole(list, paramList);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (paramList.size() > 0) {//用户角色关系信息处理
                if ("1".equals(controlType)) {
                    //添加
                    accountRoleService.insertList(paramList);
                } else if ("2".equals(controlType)) {
                    //修改
                    accountRoleService.updateList(paramList);
                } else if ("3".equals(controlType)) {
                    //删除
                    accountRoleService.deleteList(paramList);
                }
            }
        }
    }

    /**
     * 处理用户角色关系信息
     *
     * @param list
     * @param paramList
     * @throws Exception
     */
    private void accountRole(List<UasUserRole> list, List<AccountRole> paramList) throws Exception {
        if (list != null && list.size() > 0) {
            ConvertUtils.register(new DateConverter(null), Date.class);
            for (UasUserRole uur : list) {
                AccountRole param = new AccountRole();
                BeanUtils.copyProperties(param, uur);
                param.setAccountRoleId(uur.getUserRoleId());
                param.setOperatorId(uur.getUserId());
                param.setModifyer("");
                Date date = new Date();
                param.setCreatetime(date);
                param.setModifytime(date);
                param.setCreator("");
                paramList.add(param);
            }
        }
    }
}
